package sz.szsmk.citizencard.widget.lifeweb;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.ui.activity.YouzanActivity;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.JumpUtils;
import sz.szsmk.citizencard.util.StringUtils;

public class LifeWebViewClient extends WebViewClient {

    private static String HUIMINExtend;
    private Context context;
    private hideTitleBar mhideTitleBar;
    private RestorePosition restorePosition;
    private String beforeUrl = "";
    public LifeWebViewClient(Context context,hideTitleBar hideTitleBar,RestorePosition restorePosition) {
        this.context = context;
        this.mhideTitleBar=hideTitleBar;
        this.restorePosition=restorePosition;

    }


    public void setExtend(String s){
        this.HUIMINExtend=s;
    }

    @Override

    public void onPageStarted(WebView view, String url, Bitmap favicon) {

        super.onPageStarted(view, url, favicon);
        Log.d("clark","page start="+url);
//        hideTitleBar();

    }

    @Override

    public boolean shouldOverrideUrlLoading(WebView view, String url) {


        Log.d("clark", "shouldOverrideUrlLoading loadUrl=" + url + "beforeUrl=" + beforeUrl);
        String access_token = ACache.get(context).getAsString("access_token");
        if (access_token == null) {
            if (!beforeUrl.contains("toLogin")) {//屏蔽H5登录页，H5登录 原生系统并未获取到登录信息
                JumpUtils.jump(context, "login", 0);
            }
            beforeUrl = url;
        } else {
            if (url.contains("#/live/main")) {
                Log.d("clark", "reload url=" + access_token);
            } else if (url.contains("youzan")) {
                ACache.get(DaggerApplication.getIns()).remove("sykt");
                ACache.get(DaggerApplication.getIns()).put("sykt", "sykt");
                Intent intent = new Intent(context, YouzanActivity.class);
                intent.putExtra(YouzanActivity.KEY_URL, url);
                context.startActivity(intent);
            } else if (url.contains("whxfmallList")) {
                Bundle bundle = new Bundle();
                bundle.putString("stt", "stt");
                String murl="";
                if (url.contains("?")) {
                    murl = url + "&" + Constant.EXTENT;
                } else {
                    murl = url + "?" + Constant.EXTENT;
                }
                bundle.putString("url", murl);
                JumpUtils.jumptoNatvie(context, "webView", 0, bundle);
            } else {
                Bundle bundle = new Bundle();
                bundle.putString("url", url);
                JumpUtils.jumptoNatvie(context, "webView", 0, bundle);
            }
            beforeUrl = url;
        }
        return true;//返回ture 则浏览器不加载url，返回false则浏览器加载
    }

    @Override

    public void onPageFinished(WebView view, String url) {

        super.onPageFinished(view, url);
        String access_token = ACache.get(context).getAsString("access_token");
        Log.d("clark", "pageFinish=" + url);

        if (url.contains("app-front-end/#/card") || url.contains("app-front-end/#/live/play") || url.contains("app-front-end/#/live/record")) {
//            if (StringUtils.isEmpty(access_token)) {
//                JumpUtils.jump(context, "login", 0);
//            }
            mhideTitleBar.HideTitleBar("hide", "");
        } else if (url.contains("#/live/main")) {
            mhideTitleBar.HideTitleBar("show", "");
            beforeUrl = url;//回到主页或者加载新的页面之后 该参数复位，不包含toLogin
            restorePosition.restorePos();
        }
    }

    @Override

    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

        super.onReceivedError(view, errorCode, description, failingUrl);

    }

    public interface hideTitleBar {

        void HideTitleBar(String code, String message);
    }

    public interface RestorePosition{
        void restorePos();
    }

    public static Activity getCurrentActivity() {
        try {
            Class activityThreadClass = Class.forName("android.app.ActivityThread");
            Object activityThread = activityThreadClass.getMethod("currentActivityThread").invoke(
                    null);
            Field activitiesField = activityThreadClass.getDeclaredField("mActivities");
            activitiesField.setAccessible(true);
            Map activities = (Map) activitiesField.get(activityThread);
            for (Object activityRecord : activities.values()) {
                Class activityRecordClass = activityRecord.getClass();
                Field pausedField = activityRecordClass.getDeclaredField("paused");
                pausedField.setAccessible(true);
                if (!pausedField.getBoolean(activityRecord)) {
                    Field activityField = activityRecordClass.getDeclaredField("activity");
                    activityField.setAccessible(true);
                    Activity activity = (Activity) activityField.get(activityRecord);
                    return activity;
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

}


